<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
</head>
<script type="text/javascript" src="js/jquery-2.0.3.min.js"></script>
<style type="text/css">
*{
    padding: 0;
    margin: 0;
}
img {
    display: block;
    width: 500px;
}
.card{
    display: inline-block;
    position: relative;
    width: 400px;
    height: 250px;
}
.card img{
	width: 100%;
}
.card .down_btn_a {
    display: inline-block;
    position: absolute;
    right: 0;
    bottom: 0;
    width: 100px;
    height: 40px;
    line-height: 40px;
    background-color: #20b1aa;
    text-decoration: none;
    text-align: center;
    color: #fff;
}
.card .down_btn_a:hover {
    background-color: #ccc;
    color: #000;
}
</style>
<body>
	<div class="card">
    <img id="testimg" src="https://pic1.zhimg.com/v2-53f365acc16201ef0248646a3caf55e1_1200x500.jpg"/>
    <a href="javascript:;" class="down_btn_a">点击下载</a>
</div>
<div class="card">
    <img id="testimg" src="images/5.jpg"/>
    <a href="javascript:;" class="down_btn_a">点击下载</a>
</div>
</body>
<script type="text/javascript">
//判断是否为Trident内核浏览器(IE等)函数
function browserIsIe() {
    if (!!window.ActiveXObject || "ActiveXObject" in window){
        return true;
    }
    else{
        return false;
    }
}
//创建iframe并赋值的函数,传入参数为图片的src属性值.
function createIframe(imgSrc) {
    //如果隐藏的iframe不存在则创建
    if ($("#IframeReportImg").length === 0){
        $('<iframe style="display:none;" id="IframeReportImg" name="IframeReportImg" onload="downloadImg();" width="0" height="0" src="about:blank"></iframe>').appendTo("body");
    }
    //iframe的src属性如不指向图片地址,则手动修改,加载图片
    if ($('#IframeReportImg').attr("src") != imgSrc) {
        $('#IframeReportImg').attr("src",imgSrc);
    } else {
        //如指向图片地址,直接调用下载方法
        downloadImg();
    }
}
//下载图片的函数
function downloadImg() {
	alert("1")
    //iframe的src属性不为空,调用execCommand(),保存图片
    if ($('#IframeReportImg').src != "about:blank") {
    	
        window.frames["IframeReportImg"].document.execCommand("SaveAs");
    }
}
//接下来进行事件绑定
var aBtn = $(".card .down_btn_a");
if (browserIsIe()) {
    //如果浏览器是ie等,绑定事件
    aBtn.on("click", function() {
    	var imgSrc = $(this).siblings("img").attr("src");
    	//调用创建iframe的函数
        createIframe(imgSrc);
    });
} else {
    aBtn.each(function(i,v){
	//支持download,添加属性.
	var imgSrc = $(v).siblings("img").attr("src");
	$(v).attr("download",imgSrc);
	$(v).attr("href",imgSrc);
    })
}
</script>
</html>